﻿using System;

namespace CSharPro._0930;

public class Demo
{
    // 辅助方法：安全地读取整数
    private int ReadInteger(string prompt)
    {
        int result;
        while (true)
        {
            Console.Write(prompt);
            string input = Console.ReadLine();
            
            // 检查输入是否为空
            if (string.IsNullOrWhiteSpace(input))
            {
                Console.WriteLine("输入不能为空，请重新输入！");
                continue;
            }
            
            // 尝试转换为整数
            if (int.TryParse(input, out result))
            {
                return result;
            }
            else
            {
                Console.WriteLine("输入格式不正确，请输入一个有效的整数！");
            }
        }
    }

    public void TestPrint1()
    {
        Console.WriteLine("问题1：请输入三个整数：");
        // 使用带验证的方法读取输入
        int x = ReadInteger("请输入第一个整数：");
        int y = ReadInteger("请输入第二个整数：");
        int z = ReadInteger("请输入第三个整数：");

        int temp;
        // 排序逻辑
        if (x > y)
        {
            temp = x;
            x = y;
            y = temp;
        }
        if (x > z)
        {
            temp = x;
            x = z;
            z = temp;
        }
        if (y > z)
        {
            temp = y;
            y = z;
            z = temp;
        }

        Console.WriteLine("由小到大排序为：{0} {1} {2}\n", x, y, z);
    }

    public void TestPrint2()
    {
        Console.WriteLine("问题2：1-100之间所有不是7的倍数和不包含7的数字：");
        int sum = 0;
        for (int i = 1; i <= 100; i++)
        {
            if (i % 7 != 0 && i % 10 != 7 && i / 10 != 7)
            {
                Console.Write($"{i} ");
                sum += i;
            }
        }
        Console.WriteLine($"\n这些数字的和为：{sum}\n");
    }

    public void TestPrint3()
    {
        Console.WriteLine("问题3：九九乘法表：");
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write($"{j}×{i}={i * j}\t");
            }
            Console.WriteLine();
        }
        Console.WriteLine();
    }

    public void TestPrint4()
    {
        Console.WriteLine("问题4：1000以内的所有完数：");

        for (int num = 2; num <= 1000; num++)
        {
            int sum = 1; 
            for (int i = 2; i * i <= num; i++)
            {
                if (num % i == 0)
                {
                    sum += i;
                    if (i != num / i)
                    {
                        sum += num / i;
                    }
                }
            }

            if (sum == num)
            {
                Console.Write($"{num} ");
            }
        }
        Console.WriteLine("\n");
    }

    public void TestPrint5()
    {
        // 使用带验证的方法读取输入
        int num = ReadInteger("问题5：请输入一个正整数：");
        int temp = num;
        int digits = 0;

        if (temp == 0)
        {
            digits = 1;
        }
        else
        {
            while (temp != 0)
            {
                temp /= 10;
                digits++;
            }
        }

        Console.WriteLine($"{num}是{digits}位数");
        Console.Write("逆序打印各位数字：");
        temp = num;
        while (temp != 0)
        {
            Console.Write($"{temp % 10} ");
            temp /= 10;
        }
        Console.WriteLine("\n");
    }

    public void TestPrint6()
    {
        // 使用带验证的方法读取输入
        int n = ReadInteger("问题6：请输入菱形的行数(奇数)：");

        if (n % 2 == 0)
        {
            n++;
            Console.WriteLine($"自动调整为奇数：{n}");
        }

        for (int i = 1; i <= (n + 1) / 2; i++)
        {
            for (int j = 1; j <= (n + 1) / 2 - i; j++)
            {
                Console.Write(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }

        for (int i = (n - 1) / 2; i >= 1; i--)
        {
            for (int j = 1; j <= (n + 1) / 2 - i; j++)
            {
                Console.Write(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }
        Console.WriteLine();
    }

    public static void Main2(string[] args)
    {
        try
        {
            Demo demo = new Demo();
            demo.TestPrint1();
            demo.TestPrint2();
            demo.TestPrint3();
            demo.TestPrint4();
            demo.TestPrint5();
            demo.TestPrint6();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误：{ex.Message}");
        }

        Console.WriteLine("所有问题执行完毕，按任意键退出...");
        Console.ReadKey();
    }
}
    